/**
 *	Amantes Script
 *	@author Ncomunicaciones
 */

(function() {

// vars	
var $window = $(window),
	$content = $("#content"),
	$footer = $("#footer"),
	lang =  (window.location.href.indexOf("-eng") > -1 ) ? "en" : "es";



function embedVideo() {	
	if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
   		return;
	}
	
	var flashWidth = 720,
		flashHeight = 481,
		flashvars = {},
		params = {},
		attributes = {},
		video = {
			en: "http://www.youtube.com/watch?v=pkpmR4VKUqU",
			es: "http://www.youtube.com/watch?v=fBDuKNmL0Bo"
		};
		
	flashvars.videoURL = video[lang];
	flashvars.autoPlay = "false";
	flashvars.previewImg = "";
	flashvars.useFullScreen = "true";
	flashvars.useShareButtons = "true";
	flashvars.useShadow = "false";
	// Share settings
	flashvars.urlToShare = "";
	flashvars.shareTitle = "";
	flashvars.shareNotes = "";
	flashvars.twitterText = "";
	flashvars.shareLabels = "";
	flashvars.shareSource = "";
	flashvars.mailSubject = "";
	flashvars.mailBody = "";
	
	params.allowScriptAccess = "always";
	params.allowFullScreen = "true";
	params.wmode = "opaque";
	swfobject.embedSWF("swf/HandDrawnVideoPlayer.swf", "player", flashWidth, flashHeight, "9.0.0", "swf/expressInstall.swf", flashvars, params, attributes);
}

// resize
$window.on({
	resize: function() {
		var $home = $content.children("div.container"),
			windowHeight = $window.height() - $footer.innerHeight(),
			homeHeight = $home.height(),
			margin = windowHeight - homeHeight;
		margin = margin > 40 ? margin * 0.5 : "";
		$home.css({"margin-top": margin});			
	},
	load: function() {
		$(this).trigger("resize");
		$content.find("div.pages-text").mCustomScrollbar({
			scrollButtons:{enable:true},
			advanced:{updateOnContentResize:true},
			theme:"dark-2"
		});
	}
});

// player
if($("#player").length) embedVideo();
$("#video").find("div.home-video").add("a.btn-video").on("click", function(e) {
	$("div.player-wrapper").stop(true).slideDown();
	$("#video").find("div.home-video").hide();
	e.preventDefault();
});
$("#video").find("a.close").on("click", function() {
	$("div.player-wrapper").stop(true).slideUp(function() {
		var iframe = {
			en: '<div id="player"><iframe width="720" height="448" src="http://www.youtube.com/embed/pkpmR4VKUqU?showinfo=0" frameborder="0" allowfullscreen></iframe></div>',
			es: '<div id="player"><iframe width="720" height="448" src="http://www.youtube.com/embed/fBDuKNmL0Bo?showinfo=0" frameborder="0" allowfullscreen></iframe></div>'
		}
		$("#player").replaceWith(iframe[lang]);
		embedVideo();
		$("#video").find("div.home-video").show();
	});
});

// skin
$("input:file").each(function() {
	var $this = $(this),
		placeholder = $this.attr("placeholder");
	$this.wrap('<span class="file" />').css({"opacity": 0, "position": "absolute"}).after('<span>' + placeholder + '</span>');
}).on("change", function() {
	$(this).next().text($(this).val());
});

// tooltips
$("img.trigger").tooltip({effect: "slide", position: "center left", relative: true});
$("img.form-more").tooltip({effect: "slide", position: "top left", relative: true});
$("a.trigger").tooltip({effect: "slide", position: "top right", relative: true});
$("a[rel]").overlay({top: "center", mask: {color: '#000000', loadSpeed: 200, opacity: 0.9}});
// test tooltips
$("div.bar[title]").tooltip({effect: "slide", position:"top right", tipClass: "tooltip test-cloud"});
$("a.info[title]").tooltip({effect: "slide", position:"bottom center", tipClass: "tooltip data-cloud"});
$("a.info2[title]").tooltip({effect: "slide", position:"top center", tipClass: "tooltip data-cloud"});

// datos
$("a.data-prev").on("click", function() {
	var index = $("div.pane:visible").prev().index("div.pane");
	$("a.data-next").show();
	if(index < 1) $(this).hide();
	$("div.pane").hide().eq(index).show();	
});
$("a.data-next").on("click", function() {
	var index = $("div.pane:visible").next().index("div.pane");
	$("a.data-prev").show();
	if(index === $("div.pane").length - 1) $(this).hide();
	$("div.pane").hide().eq(index).show();
});


$("a.data-prev2").on("click", function() {
	var index = $("div.pane:visible").prev().index("div.pane");
	$("a.data-next2").show();
	if(index < 1) $(this).hide();
	$("div.pane").hide().eq(index).show();	
});
$("a.data-next2").on("click", function() {
	var index = $("div.pane:visible").next().index("div.pane");
	$("a.data-prev2").show();
	if(index === $("div.pane").length - 1) $(this).hide();
	$("div.pane").hide().eq(index).show();
});

// historia
$("form.upload").on("submit", function(e) {
	var $form = $(this),
		formData = $form.serialize(),
		isValid = true;
	e.preventDefault();
	if(!$form.find("input:checkbox").prop("checked")) {
		alert("Usted debe marcar el acuerdo de Términos y condiciones.");
		return;
	}
	$form.find("input:text, textarea").each(function() {
		var val = $(this).val(),
			name = $(this).attr("name");
		if(!val && name !== "url") {
			$(this).css("border-color", "red");
			isValid = false;
		}
		else {
			$(this).css("border-color", "");
		}
	});
	if(!isValid) return;
	$form.prepend('<p class="form-msg"><span style="color:#999;">Verificando sus datos ...</span></p>');
	$form.find("p.form-msg").siblings().hide();
	$.post("tu_historia_handler.php", formData, function(data) {
		$form.html(data);
	});
});

})();


/*GOOGLE ANALITICS*/
if(window.location.href.indexOf("imalover.org")> -1) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 

  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new 
	Date();a=s.createElement(o),
	m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 
   })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); 
	ga('create', 'UA-28293532-3', 'imalover.org');
	ga('send', 'pageview');
}else{
	(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
	})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
	   ga('create', 'UA-28293532-2', 'soyamante.org');
	   ga('send', 'pageview');
   }